#include "HNGame.h"
#include "Game/Script/ScriptData.h"
#include "Game/Script/utility.h"
#include "Kernel/socket/Socket.h"
#include "Scene/HNScenceManager.h"
#include "Game/Script/ScriptXMLparse.h"
USING_NS_CC;

CCScene* HNGame::scene()
{
    CCScene *scene = CCScene::create();
    HNGame *layer = HNGame::create();
    scene->addChild(layer);
    return scene;
}
void HNGame::setString(std::string kTxt)
{
}
bool HNGame::init()
{
    if ( !CCLayer::init() )
    {
        return false;
	}

	auto listener2 = EventListenerKeyboard::create();
	listener2->onKeyReleased = CC_CALLBACK_2(HNGame::onKeyReleased, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener2, this);

#if CC_TARGET_PLATFORM != CC_PLATFORM_IOS
	Sprite* spriteBg = Sprite::create("common/loading.png");
	addChild(spriteBg);
	spriteBg->setPosition(Vec2(Director::getInstance()->getWinSize().width / 2, Director::getInstance()->getWinSize().height / 2));
	spriteBg->runAction(Sequence::create(DelayTime::create(3.0f), RemoveSelf::create(), NULL));
#endif
	

	cocos2d::ScriptXMLparse kScriptXml1("ScriptValue.xml");
	cocos2d::ScriptXMLparse kScriptXml2("ScriptValueStr.xml");
	
	this->scheduleUpdate();
	this->setTouchEnabled(true);
	this->setTouchMode(Touch::DispatchMode::ALL_AT_ONCE);

	scheduleOnce(schedule_selector(HNGame::delayOnEnter), 0.1f);
    return true;
}

void HNGame::onKeyReleased(EventKeyboard::KeyCode keyCode, Event * pEvent)
{
	if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_ESCAPE)
	{
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
		JniMethodInfo minfo;
		bool has = JniHelper::getStaticMethodInfo(minfo,
			"org/cocos2dx/cpp/AppActivity",
			"gameExit", "()V");
		if (!has) {
			CCLog("jni:dont have this method.");
		}
		else {
			minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
		}
#endif
	}
}

void HNGame::delayOnEnter(float dt)
{
	new HNScenceManager(this);
}

void HNGame::onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event)
{
	if (touches.size() == 0)
	{
		return;
	}
	
}
void HNGame::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event)
{
	if (touches.size() == 0)
	{
		return;
	}
}
void HNGame::onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event *unused_event)
{
	if (touches.size() == 0)
	{
		return;
	}
}
void HNGame::onTouchesCancelled(const std::vector<cocos2d::Touch*>&touches, cocos2d::Event *unused_event)
{
	if (touches.size() == 0)
	{
		return;
	}
}

void HNGame::update(float delta)
{
}
void HNGame::onExit()
{
	cocos2d::Layer::onExit();
}
void HNGame::ExitGame()
{
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
